package org.jeecg.desen.run.service.impl;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.jeecg.desen.run.component.DscConversionService;
import org.jeecg.desen.run.component.hive.DscHiveConversionService;
import org.jeecg.desen.run.entity.DscSsc;
import org.jeecg.desen.run.entity.DscSscFieldDetail;
import org.jeecg.desen.run.entity.DscSscTableDetail;
import org.jeecg.desen.run.service.DesManualRunService;
import org.jeecg.desen.run.service.DesRunService;
import org.jeecg.modules.meta.entity.SysTableinfo;
import org.jeecg.modules.rule.entity.OpersscFiddetail;
import org.jeecg.modules.rule.entity.OpersscFo;
import org.jeecg.modules.rule.entity.OpersscTabdetail;
import org.jeecg.modules.rule.service.IOpersscFiddetailService;
import org.jeecg.modules.rule.service.IOpersscFoService;
import org.jeecg.modules.rule.service.IOpersscTabdetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/***
 * 手工脱敏运行实现类。
 */
@Slf4j
@Service
public class DesRunManualService extends AbstractDesRunService implements DesManualRunService {
    private String sscid;

    @Autowired
    IOpersscFoService iOpersscFoService;

    @Autowired
    IOpersscTabdetailService iOpersscTabdetailService;

    @Autowired
    IOpersscFiddetailService iOpersscFiddetailService;

    @Resource(name = "dscManualHiveConversionServiceImpl")
    DscHiveConversionService dscConversionService;

    @Override
    public DesRunService bindSscid(String sscid, Date runDate) {
        log.info("手工脱敏bind参数,订阅id=" + sscid);
        this.sscid = sscid;
        this.runDate = runDate;
        return this;
    }

    @Override
    public DesRunService bindSscid(String sscid, Date runDate, Date endDate) {
        this.sscid = sscid;
        this.runDate = runDate;
        this.endDate = endDate;
        return this;
    }

    @Override
    List<DscSsc> getDscSsc() {
        //根据订阅id获取手工订阅信息
        OpersscFo opersscFo = getManualSscList(sscid);
        //获取订阅相关的表信息
        DscSsc dscSsc = getDscSscEntity(opersscFo);
        //进行自动订阅表信息查询
        sscQueryAutoTable(dscSsc);
        return Arrays.asList(dscSsc);
    }

    @Override
    DscSsc getRunDscSsc(String sscId) {
        //根据订阅id获取手工订阅信息
        OpersscFo opersscFo = getManualSscList(sscid);
        //获取订阅相关的表信息
        DscSsc dscSsc = getDscSscEntity(opersscFo);
        //进行自动订阅表信息查询
        sscQueryAutoTable(dscSsc);
        return dscSsc;
    }

    @Override
    DscConversionService getComponent() {
        if (null != endDate) {
            return dscConversionService.bindParam(sscid, runDate, endDate);
        } else {
            return dscConversionService.bindParam(sscid, runDate);
        }
    }

    @Override
    void setTableField(DscSscTableDetail dscSscTableDetail) {
        //根据表id获取field字段集合
        String tableId = dscSscTableDetail.getFdTableId();
        dscSscTableDetail.setFieldDetailList(convSscFieldDetail(getFieldList(tableId,sscid)));
    }

    @Override
    void setWhere(DscSscTableDetail dscSscTableDetail) {
        String tableId = dscSscTableDetail.getFdTableId();
        SysTableinfo sysTableinfo = getSysTableinfo(tableId);
        if (null != sysTableinfo) {
            dscSscTableDetail.setFdLoaddt(sysTableinfo.getFdLoaddt());
            dscSscTableDetail.setFdLoaddt2(sysTableinfo.getFdPartition2());
            dscSscTableDetail.setFdType(sysTableinfo.getFdType());
            dscSscTableDetail.setFdTableName(sysTableinfo.getFdTablename());
        }
    }

    @Override
    boolean verificationParam() {
        if (StringUtils.isEmpty(sscid) || runDate == null) {
            return false;
        }
        return true;
    }

    @Override
    String getParam() {
        return "自动脱敏参数,sscid=" + sscid + ",runDate=" +
                (null != runDate ? DateUtil.formatDate(runDate) : "") + ",endDate=" +
                (null != endDate ? DateUtil.formatDate(endDate) : "");
    }


    private void sscQueryAutoTable(DscSsc dscSsc) {
        dscSsc.setTableDetailList(convSscDetail(getOpersscTabdetailListDetail(dscSsc.getSscid()),dscSsc));
    }

    private List<DscSscTableDetail> convSscDetail(List<OpersscTabdetail> opersscTabdetails,DscSsc dscSsc) {
        List<DscSscTableDetail> dscSscTableDetails = new ArrayList<>();
        for (OpersscTabdetail opersscTabdetail : opersscTabdetails) {
            DscSscTableDetail dscSscTableDetail = new DscSscTableDetail();
            dscSscTableDetail.setFdTableId(opersscTabdetail.getFdTableId());
            dscSscTableDetail.setFdWhere(opersscTabdetail.getFdWhere());
            dscSscTableDetail.setSscid(opersscTabdetail.getFdSscid());
            dscSscTableDetail.setFdSource(dscSsc.getFdSource());//默认跟随订阅信息里面的脱敏地址，如果增加表级处理，需要增加判断
            dscSscTableDetails.add(dscSscTableDetail);
        }
        return dscSscTableDetails;
    }

    private List<OpersscTabdetail> getOpersscTabdetailListDetail(String sscid) {
        QueryWrapper<OpersscTabdetail> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("fd_sscid", sscid);
        queryWrapper.eq("fd_status", 1);
        return iOpersscTabdetailService.list(queryWrapper);
    }

    private DscSsc getDscSscEntity(OpersscFo  opersscFo) {
        DscSsc dscSsc = new DscSsc();
        dscSsc.setSscid(opersscFo.getId());//订阅id
        dscSsc.setSscName(opersscFo.getFdName());//订阅名称
        dscSsc.setSscType(2);//类型是否是自动
        dscSsc.setExpain(opersscFo.getFdExpain());//说明
        dscSsc.setFdProject(opersscFo.getFdProject());//应用
        dscSsc.setFdSource(opersscFo.getFdSource());
        return dscSsc;
    }

    private OpersscFo getManualSscList(String sscId) {
        //获取频次对应的订阅信息 ，当个频次可以对应
        QueryWrapper<OpersscFo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", sscId);
        queryWrapper.eq("fd_status", 1);
        return iOpersscFoService.getOne(queryWrapper);
    }



    List<OpersscFiddetail> getFieldList(String tableId,String sscid) {
        QueryWrapper<OpersscFiddetail> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("fd_table_id", tableId);
        queryWrapper.eq("fd_status", "1");
        queryWrapper.eq("fd_sscid", sscid);
        return iOpersscFiddetailService.list(queryWrapper);
    }

    private List<DscSscFieldDetail> convSscFieldDetail(List<OpersscFiddetail> opersscFiddetails) {
        List<DscSscFieldDetail> dscSscTableDetails = new ArrayList<>();
        for (OpersscFiddetail opersscFiddetail : opersscFiddetails) {
            DscSscFieldDetail dscSscFieldDetail = new DscSscFieldDetail();
            dscSscFieldDetail.setFdFieldid(opersscFiddetail.getFdFieldid());
            dscSscFieldDetail.setFdFieldname(opersscFiddetail.getFdFieldname());
            dscSscFieldDetail.setFdRulelevel(opersscFiddetail.getFdRulelevel());
            dscSscFieldDetail.setFdRulename(opersscFiddetail.getFdRulename());
            dscSscFieldDetail.setFdDes("1");
            dscSscTableDetails.add(dscSscFieldDetail);
        }
        return dscSscTableDetails;
    }

}
